package com.example.classes;

import android.util.Log;
import com.google.gson.annotations.Expose;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Represents a Member account.
 * @author Ali Abid, Alexander Huynh
 */
public class Member  implements Serializable
{
    @Expose
	private String name, password, contact;
    @Expose
    private int strikes;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Member)) return false;

        Member member = (Member) o;

        if (!contact.equals(member.contact)) return false;
        if (!name.equals(member.name)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = name.hashCode();
        result = 31 * result + contact.hashCode();
        return result;
    }

    @Expose

    private List<Item> items = new ArrayList<Item>();

    /**
     * Constructor.
     * Instantiates and sets values for private fields.
     * @param userName string for the name of the account
     * @param pass string for the password of the account
     * @param cont string for the contact information
     */
    Member(String userName, String pass, String cont)
    {
        name = userName;
        password = pass;
        contact = cont;
        strikes = 0;
    }

    /**
     * Constructor.
     * Instantiates and sets values for private fields.
     * @param userName string for the name of the account
     * @param pass string for the password of the account
     * @param cont string for the contact information
     * @param locks number of incorrect login attempts
     */
    Member(String userName, String pass, String cont, int locks)
    {
        name = userName;
        password = pass;
        contact = cont;
        strikes = locks;
    }

    Member(){}

    /**
     * Gets the items created by this Member.
     * @return List of items created by this Member
     */
    public List<Item> getItems()
    {
        return items;
    }

    /**
     * Defines the list of items lost by this Member.
     * @param items List of items lost
     */
    public void setItems(List<Item> items)
    {
        this.items = items;
    }

    /**
     * Gives the locked state of Member.
     * @return true if the Member is locked, false otherwise
     */
    public boolean isLocked()
    {
        return strikes >= 3;
    }

    /**
     * Increments number of incorrect logins on this user.
     */
    public void incorrectLogin()
    {
        Log.v("WMS-DEBUG","MEMBER: incrementing strikes by one. (" + strikes + "->" + (strikes+1) + ")");
        strikes++;
    }

    /**
     * Resets number of incorrect logins on this user.
     */
    public void resetStrikes()
    {
        Log.v("WMS-DEBUG","MEMBER: resetting strikes to zero.");
        strikes = 0;
    }

    /**
     * Gives number of incorrect logins on this user.
     * @return number of incorrect logins on this user
     */
    public int getStrikes()
    {
        return strikes;
    }

    /**
    * Returns the username of the Member.
	* @return the username
	*/
	public String getName()
	{
		return name;
	}

    /**
    * Returns the password of the Member.
	* @return the password
	*/
	public String getPassword()
	{
		return password;
	}

    /**
    * Returns the contact info of Member
	* @return the contact info
	*/
	public String getContact()
	{
		return contact;
	}

	/**
	 * Adds the given item to this Member's Item list.
	 * @param i item to add to the list
	 */
	public void addItem(Item i)
	{
		items.add(i);
	}

	/**
	 * Removes the given item from this Member's Item list.
	 * @param i item to remove from list
	 */
	public void removeItem(Item i)
	{
		items.remove(i);
	}

    /**
     * Returns debug info for this Member
     * @return info about this Member
     */
    public String toString()
    {
        String res = "\n\nTYPE: ";
        if (this instanceof User)
            res += "User\n";
        else res += "Admin\n";
        res += "User name: " + this.name+ "\nITEMS:\n";
        int i = 0;
        for(Item e : items)
        {
            res += (i++) + " " + e.toString() + "-> "+(e.isState()?"FOUND":"LOST")+"\n";
        }
        return res;
    }
}